package org.hbbTlias2025.exception;


import lombok.extern.slf4j.Slf4j;
import org.hbbTlias2025.pojo.Result;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class globalExceptionHandler {
    @ExceptionHandler
    public Result handelException(Exception e){
        log.error("服务器发生异常：{}",e.getMessage());
        return Result.error("服务器发生异常{}" + e.getMessage());
    }

    @ExceptionHandler
    public Result handelKeyException(DuplicateKeyException e){
        log.error("出现了重复值：{}",e.getMessage());
        String message = e.getMessage();
        int position = message.indexOf("Duplicate entry");
        message = message.substring(position);
        String[] messageError = message.split(" ");

        return Result.error("出现了重复值+" + messageError[2]);
    }
}
